package com.fjb.controller.index;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fjb.common.plugins.ZTree;
import com.fjb.controller.base.BaseController;
import com.fjb.pojo.auth.SysPermission;
import com.fjb.pojo.auth.SysRole;
import com.fjb.pojo.login.LoginUserInfo;
import com.fjb.service.auth.SysPermissionService;
import com.fjb.service.auth.SysRoleService;

/**
 * @Description: java类作用描述
 * @Author: hm
 * @CreateDate: 2018/12/24 21:19
 * @Version: 1.0
 */
@Controller
public class IndexController extends BaseController{
	
	@Autowired
	private SysPermissionService sysPermissionService;
	@Autowired
	private SysRoleService sysRoleService;
	
	/**
	 * @Description:默认页面
	 * @param request
	 * @return
	 * String
	 * @exception:
	 * @author: hemiao
	 * @time:2019年5月6日 下午4:18:15
	 */
    @RequestMapping(value = "/")
    public String showDefault(HttpServletRequest request){
    	return "/login/login";
    }
	
	/**
	 * @Description:主页面
	 * @return
	 * String
	 * @exception:
	 * @author: hemiao
	 * @time:2019年4月20日 下午8:54:45
	 */
    @RequestMapping(value = "/index")
    public ModelAndView showIndex(HttpServletRequest request){
    	ModelAndView mv = new ModelAndView();
    	LoginUserInfo loginUserInfo = getUserInfo(request);
    	mv.addObject("fjbLoginUserInfo", loginUserInfo);	
    	// 获得所有的角色
    	QueryWrapper<SysRole> roleWrapper = new QueryWrapper<SysRole>();
    	List<SysRole> validRolelist = sysRoleService.list(roleWrapper);
    	mv.addObject("validRolelist", validRolelist);
    	
    	// 获得所有的菜单列表	
    	List<ZTree> permissionList = sysPermissionService.selectValidRoleIdPermissionList(1992);
    	// 菜单列表
		List<ZTree> validMenuList = new ArrayList<ZTree>();
		if(permissionList.size()>0) {
			// 取出所有的1级菜单
			for (ZTree zTree : permissionList) {
				Integer level = zTree.getLevel();
				if(level==1) {
					validMenuList.add(zTree);
				}
			}
			// 获得 1级下的子菜单
			for (ZTree zTree : validMenuList) {
				Integer id = zTree.getId();
				List<ZTree> childList = getChildList(id, permissionList);
				if(childList.size()>0) {
					zTree.setChildList(childList);
					zTree.setNoChild(false);
				}
			}
		}		
    	mv.addObject("validMenuList", validMenuList);
    		
    	mv.setViewName("/index");
        return mv;
    }
    
    /**
     * @Description:登录
     * @return
     * String
     * @exception:
     * @author: hemiao
     * @time:2019年4月20日 下午8:54:21
     */
    @RequestMapping(value = "/login")
    public String showLogin(){
        return "/login/login";
    }
    
    /**
     * @Description:注册
     * @return
     * String
     * @exception:
     * @author: hemiao
     * @time:2019年4月20日 下午8:54:17
     */
    @RequestMapping(value = "/register")
    public String showRegister(){
        return "/login/register";
    }
    
    @RequestMapping(value = "/console")
    public String showConsole(){
        return "/console";
    }
}
